from PIL import Image
import matplotlib.pyplot as plt
import numpy as np


# 获取图片
def getimg(img_path):
    return Image.open(img_path)


# 显示图片
def showimg(img, isgray=False):
    plt.axis("off")
    if isgray == True:
        plt.imshow(img, cmap='gray')
    else:
        plt.imshow(img)
    plt.show()


# img_name = "cut"
# img_name = "cut_1112101839-005"
# img_name = "cut_1112101839-007"
# img_name = "cut_1112101839-027"
# img_suffix = ".jpg"
# img_path = "../pic/" + img_name + img_suffix
# im = getimg(img_path)

# 取中间值灰度化，效果不行
# im_gray1 = im.convert('L')
# im_gray1 = np.array(im_gray1)
# avg_gray = np.average(im_gray1)
# im_gray1 = np.where(im_gray1[..., :] < avg_gray, 0, 255)
# showimg(Image.fromarray(im_gray1), True)

# 反色
img_path = r'../pic/pillow/cut_1112101839-027_convert_235.jpg'
im = getimg(img_path)
im_gray = im.convert('L')
im_arr = np.array(im_gray)
im1 = 255 - im_arr
showimg(Image.fromarray(im1))
